package com.android.calendartest;

import android.app.Activity;
import android.os.Bundle;
import android.content.ContentResolver;
import android.content.ContentValues; 
import android.content.ContentUris;
import android.net.Uri;
import android.text.format.Time;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;


public class CalendarTest extends Activity
{
    static final String TAG = "CalendarTest";

    private Button button;
    //private TextView view;
    private EditText editTitle;
    private EditText editDesc;
    private EditText editStart;
    private EditText editEnd;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

/*
        //int calId = insertCal("Calendar0", DEFAULT_TIMEZONE);
        int calId = 1;
        EventInfo event = new EventInfo ("normal event 0", "This is a test. Add normal event 0 to calendar", 
                "2009-11-19T14:00:00", "2009-11-19T15:00:00", true);

        Uri url = insertEvent (calId, event);

        long eventId = ContentUris.parseId(url);
        Log.d (TAG, "eventId = " + eventId);

        // 0 minute prior to the event 
        insertReminder (eventId, 0);
*/

        button = (Button) findViewById (R.id.set_cal);
        //view = (TextView) findViewById (R.id.TextView01);

        editTitle = (EditText) findViewById (R.id.edit_title);
        editDesc = (EditText) findViewById (R.id.edit_desc);
        editStart = (EditText) findViewById (R.id.edit_start);
        editEnd = (EditText) findViewById (R.id.edit_end);
 


        button.setOnClickListener (new Button.OnClickListener () {
            public void onClick (View v){
                
                String title = editTitle.getText().toString();
                String desc = editDesc.getText().toString();
                String start = editStart.getText().toString();
                String end = editEnd.getText().toString();
                int calId = 1;

                EventInfo event = new EventInfo (title, desc, start, end, true);

                Uri url = insertEvent (calId, event);

                long eventId = ContentUris.parseId(url);
                Log.d (TAG, "eventId = " + eventId);

                // 0 minute prior to the event 
                insertReminder (eventId, 0);

                /* Alert Dialog */
                new AlertDialog.Builder (CalendarTest.this)
                .setTitle ("Congradulations")
                .setMessage ("Calendar Item is set.")
                .setPositiveButton ("OK", new DialogInterface.OnClickListener (){

                    public void onClick (DialogInterface dialog, int whichButton){
                        setResult (RESULT_OK);
                        //finish ();
                    }


                })
                .show ();
            }
        });


    }


    private Uri insertEvent (int calId, EventInfo event) {

        Uri uri = Uri.parse("content://calendar/events");
        ContentResolver cr = CalendarTest.this.getContentResolver();

        ContentValues values = new ContentValues();
        values.put("calendar_id", calId);
        values.put("title", event.mTitle);
        values.put("dtstart", event.mDtstart);
        values.put("dtend", event.mDtend);
        values.put("description", event.mDescription); 
        values.put("hasAlarm", 1); 
        Uri url = cr.insert(uri, values);
/*
        long eventId = ContentUris.parseId(url);
        Log.d (TAG, "eventId = " + eventId);

        Uri uriReminder = Uri.parse ("content://calendar/reminders");
        values.clear ();
        values.put("event_id", eventId);
        values.put("minutes", 7);
        values.put("method", 1);
        url = cr.insert(uriReminder, values);
*/        

        return url; 
        
    }

        /**
         * The minutes prior to the event that the alarm should ring. 
         * <P>Type: INTEGER</P>
 
         * public static final String MINUTES = "minutes";
         * public static final int MINUTES_DEFAULT = -1;

  
         * The alarm method, as set on the server.  DEFAULT, ALERT, EMAIL, and
         * SMS are possible values; the device will only process DEFAULT and
         * ALERT reminders (the other types are simply stored so we can send the
         * same reminder info back to the server when we make changes).
         *
         * public static final String METHOD = "method";

         * public static final int METHOD_DEFAULT = 0;
         * public static final int METHOD_ALERT = 1;
         * public static final int METHOD_EMAIL = 2;
         * public static final int METHOD_SMS = 3;
         * 
         * SeeAlso
         * packages/apps/Calendar/src/com/android/calendar/EditEvent.java : saveReminders
         * frameworks/base/core/java/android/provider/Calendar.java
         **/


    private Uri insertReminder (long eventId, int minutes) {

        Uri uri = Uri.parse ("content://calendar/reminders");
        ContentResolver cr = CalendarTest.this.getContentResolver();

        ContentValues values = new ContentValues();
        values.put("event_id", eventId);
        values.put("minutes", minutes);  /* 7 min prior to the event */
        values.put("method", 1);   /* MEHTOD_ALERT */
        Uri url = cr.insert(uri, values);
        
        return url; 
        
    }


    private int insertCal(String name, String timezone) {

        Uri uri = Uri.parse("content://calendar/calendars");
        ContentResolver cr = CalendarTest.this.getContentResolver();
 

        ContentValues m = new ContentValues();
        m.put("name", name);
/*
         m.put(Calendars.DISPLAY_NAME, name);
         m.put(Calendars.COLOR, "0xff123456");
         m.put(Calendars.TIMEZONE, timezone);
         m.put(Calendars.SELECTED, 1);
*/

        Uri url = cr.insert(uri, m);
        String id = url.getLastPathSegment();
        return Integer.parseInt(id);
    }



    private class EventInfo {
        String mTitle;
        String mDescription;
        long mDtstart;
        long mDtend;
        boolean mAllDay;

        public EventInfo (String title, String description, String startDate, String endDate, boolean allDay) {

            mTitle = title;
            mDescription = description;
            

            Time time = new Time();
            time.timezone = Time.TIMEZONE_UTC;
            time.parse3339(startDate);
            mDtstart = time.toMillis(false /* use isDst */);
            time.parse3339(endDate);
            mDtend = time.toMillis(false /* use isDst */);

            boolean mAllDay = true;

        }

            
    };

    private EventInfo[] mEvents = {
        new EventInfo("normal0", "normal0 description", "2009-11-18T13:00:00", "2009-11-18T14:00:00", true),
        new EventInfo("normal1", "normal1 description", "2008-05-26T08:30:00", "2008-05-26T09:30:00", true)
    };


}
